package com.ruoyi.xp.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.GlobalConstant;
import com.ruoyi.xp.order.domain.*;
import com.ruoyi.xp.order.dto.*;
import com.ruoyi.xp.order.mapstruct.OrderMap;
import com.ruoyi.xp.order.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chendx
 */
@Service
public class OrderInfoServiceImpl implements IOrderInfoService {

    private static final Logger log = LoggerFactory.getLogger(OrderInfoServiceImpl.class);

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderIncomeService orderIncomeService;

    @Autowired
    private IOrderItemService orderItemService;

    @Autowired
    private IOrderItemGoodsService orderItemGoodsService;

    @Autowired
    private IOrderItemHeadPackageService orderItemHeadPackageService;

    @Resource
    private OrderMap orderMap;

    @Override
    public void addOrderInfo(OrderInfoDto orderInfoDto) {
        Order order = orderMap.mapOrderDtoToEntity(orderInfoDto.getOrderDto());
        order.setCreateBy(String.valueOf(GlobalConstant.COMMON_SYNC_TASK_USER_ID));
        order.setCreateDate(new Date());
        order.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
        order.setLastUpdateDate(new Date());
        orderService.insertOrder(order);
        OrderIncome orderIncome = orderMap.mapOrderIncomeDtoToEntity(orderInfoDto.getOrderIncomeDto());
        orderIncome.setCreateBy(String.valueOf(GlobalConstant.COMMON_SYNC_TASK_USER_ID));
        orderIncome.setCreateDate(new Date());
        orderIncome.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
        orderIncome.setLastUpdateDate(new Date());
        orderIncomeService.insertOrderIncome(orderIncome);
    }

    @Override
    public void addOrderItemInfo(OrderItemInfoDto orderItemInfoDto) {

        List<OrderItemDto> orderItemDtoList = orderItemInfoDto.getOrderItemDtoList();
        if (ObjectUtil.isNotNull(orderItemDtoList)) {
            for (OrderItemDto orderItemDto : orderItemDtoList) {
                OrderItem orderItem = orderMap.mapOrderItemDtoToEntity(orderItemDto);

                orderItem.setCreateBy(String.valueOf(GlobalConstant.COMMON_SYNC_TASK_USER_ID));
                orderItem.setCreateDate(new Date());
                orderItem.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
                orderItem.setLastUpdateDate(new Date());
                orderItemService.insertOrderItem(orderItem);

                List<OrderItemGoodsDto> orderItemGoodsDtoList = orderItemDto.getOrderItemGoodsDtoList();
                addOrderItemGoods(orderItemGoodsDtoList);

                List<OrderItemHeadPackageDto> orderItemHeadPackageDtoList = orderItemDto.getOrderItemHeadPackageDtoList();
                addOrderItemHeadPackage(orderItemHeadPackageDtoList);
            }
        }
        updateWarehouseStatus(orderItemInfoDto);
    }


    @Override
    public void addOrderItemGoodsOrHeadPackageInfo(OrderItemInfoDto orderItemInfoDto) {
        List<OrderItemDto> orderItemDtoList = orderItemInfoDto.getOrderItemDtoList();
        if (ObjectUtil.isNotNull(orderItemDtoList)) {
            for (OrderItemDto orderItemDto : orderItemDtoList) {
                List<OrderItemGoodsDto> orderItemGoodsDtoList = orderItemDto.getOrderItemGoodsDtoList();
                List<OrderItemHeadPackageDto> orderItemHeadPackageDtoList = orderItemDto.getOrderItemHeadPackageDtoList();
                if (CollectionUtil.isNotEmpty(orderItemGoodsDtoList) || CollectionUtil.isNotEmpty(orderItemHeadPackageDtoList)) {
                    addOrderItemGoods(orderItemGoodsDtoList);
                    addOrderItemHeadPackage(orderItemHeadPackageDtoList);
                }
            }
        }
        updateWarehouseStatus(orderItemInfoDto);
    }

    @Override
    public void updateOrderItemGoodsOrHeadPackageInfo(OrderItemInfoDto orderItemInfoDto) {
        orderItemGoodsService.deleteOrderItemGoodsByOrderSn(orderItemInfoDto.getOrderSn());
        orderItemHeadPackageService.deleteOrderItemHeadPackageByOrderSn(orderItemInfoDto.getOrderSn());
        addOrderItemGoodsOrHeadPackageInfo(orderItemInfoDto);
    }


    @Override
    public List<OrderDto> getOrderHaveNoItem() {

        List<Order> orderList = orderService.selectOrderHaveNoItem();
        return mapOrderEntityListToDtoList(orderList);
    }

    @Override
    public List<OrderItemDto> getOrderItemHaveNoGoodsOrHeadPackage() {
        List<OrderItem> orderItemList = orderItemService.selectOrderItemHaveGoodsOrHeadPackage();

        List<OrderItemDto> orderItemDtoList = new ArrayList<>();
        for (OrderItem orderItem: orderItemList) {
            OrderItemDto orderItemDto = orderMap.mapOrderItemEntityToDto(orderItem);
            orderItemDtoList.add(orderItemDto);
        }
        return orderItemDtoList;
    }

    @Override
    public List<OrderDto> getWarehouseInitOrder() {

        List<Order> orderList = orderService.selectWarehouseInitOrder();
        return mapOrderEntityListToDtoList(orderList);
    }


    /**
     * 查询仓库处理中订单
     *
     * @return
     */
    @Override
    public List<OrderDto> getWareHouseWaitProcessOrder() {

        List<Order> orderList = orderService.selectWareHouseWaitProcessOrder();
        return mapOrderEntityListToDtoList(orderList);
    }

    /**
     * 查询仓库待打包订单
     *
     * @return
     */
    @Override
    public List<OrderDto> getWareHouseWaitPackOrder() {

        List<Order> orderList = orderService.selectWareHouseWaitPackOrder();
        return mapOrderEntityListToDtoList(orderList);
    }

    @Override
    public int updateOrderStatus(OrderDto orderDto) {

        Order orderQuery = new Order();
        orderQuery.setOrderSn(orderDto.getOrderSn());
        List<Order> orderList = orderService.selectOrderList(orderQuery);
        Long orderId = null;
        if (CollectionUtil.isNotEmpty(orderList) && orderList.size() == 1) {
            orderId = orderList.get(0).getOrderId();
        }else {
            log.info("订单{}在订单表不存在或者存在多条记录", orderDto.getOrderSn());
            return 0;
        }

        Order order = orderMap.mapOrderDtoToEntity(orderDto);
        order.setOrderId(orderId);
        order.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
        order.setLastUpdateDate(new Date());
        return orderService.updateOrder(order);
    }

    @Override
    public List<OrderDto> getOrderWaitToShip() {
        List<Order> orderList = orderService.selectOrderWaitToShip();
        return mapOrderEntityListToDtoList(orderList);
    }

    @Override
    public List<OrderDto> getOrderShipping() {
        List<Order> orderList = orderService.selectOrderShipping();
        return mapOrderEntityListToDtoList(orderList);
    }

    /**
     * 查询未有运费的订单
     *
     * @return
     */
    @Override
    public List<OrderDto> getHaveNoShippingFeeOrder() {
        List<Order> orderList = orderService.selectHaveNoShippingFeeOrder();
        return mapOrderEntityListToDtoList(orderList);
    }

    /**
     * 更新订单收入
     *
     * @param orderIncomeDto
     * @return
     */
    @Override
    public int updateOrderIncome(OrderIncomeDto orderIncomeDto) {

        OrderIncome orderIncomeQuery = new OrderIncome();
        orderIncomeQuery.setOrderSn(orderIncomeDto.getOrderSn());
        List<OrderIncome> orderIncomeList = orderIncomeService.selectOrderIncomeList(orderIncomeQuery);
        Long orderIncomeId = null;
        if (CollectionUtil.isNotEmpty(orderIncomeList) && orderIncomeList.size() == 1) {
            orderIncomeId = orderIncomeList.get(0).getOrderIncomeId();
        }else {
            log.info("订单{}在订单收入表不存在或者存在多条记录", orderIncomeDto.getOrderSn());
            return 0;
        }

        OrderIncome orderIncome = orderMap.mapOrderIncomeDtoToEntity(orderIncomeDto);
        orderIncome.setOrderIncomeId(orderIncomeId);
        orderIncome.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
        orderIncome.setLastUpdateDate(new Date());
        return orderIncomeService.updateOrderIncome(orderIncome);

    }

    @Override
    public void updatePurchasePrice(String orderSn) {
        orderItemService.updatePurchasePrice(orderSn);
    }

    @Override
    public List<OrderIncomeDto> getHaveNoWarehousePackageAmountOrder() {

        List<OrderIncome> orderIncomeList = orderIncomeService.selectOrderHaveNoWarehousePackageAmount();
        List<OrderIncomeDto> orderIncomeDtoList = new ArrayList<>();
        for (OrderIncome orderIncome : orderIncomeList) {
            OrderIncomeDto orderIncomeDto = orderMap.mapOrderIncomeEntityToDto(orderIncome);
            orderIncomeDtoList.add(orderIncomeDto);
        }
        return orderIncomeDtoList;
    }


    private void addOrderItemGoods(List<OrderItemGoodsDto> orderItemGoodsDtoList) {
        if (ObjectUtil.isNotNull(orderItemGoodsDtoList)) {
            for (OrderItemGoodsDto orderItemGoodsDto : orderItemGoodsDtoList) {
                OrderItemGoods orderItemGoods = orderMap.mapOrderItemGoodsDtoToEntity(orderItemGoodsDto);
                orderItemGoods.setCreateBy(String.valueOf(GlobalConstant.COMMON_SYNC_TASK_USER_ID));
                orderItemGoods.setCreateDate(new Date());
                orderItemGoods.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
                orderItemGoods.setLastUpdateDate(new Date());
                orderItemGoodsService.insertOrderItemGoods(orderItemGoods);
            }
        }
    }

    private void addOrderItemHeadPackage(List<OrderItemHeadPackageDto> orderItemHeadPackageDtoList) {
        if (ObjectUtil.isNotNull(orderItemHeadPackageDtoList)) {
            for (OrderItemHeadPackageDto orderItemHeadPackageDto : orderItemHeadPackageDtoList) {
                OrderItemHeadPackage orderItemHeadPackage = orderMap.mapOrderItemHeadPackageDtoToEntity(orderItemHeadPackageDto);
                orderItemHeadPackage.setCreateBy(String.valueOf(GlobalConstant.COMMON_SYNC_TASK_USER_ID));
                orderItemHeadPackage.setCreateDate(new Date());
                orderItemHeadPackage.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
                orderItemHeadPackage.setLastUpdateDate(new Date());
                orderItemHeadPackageService.insertOrderItemHeadPackage(orderItemHeadPackage);
            }
        }
    }

    private void updateWarehouseStatus(OrderItemInfoDto orderItemInfoDto) {
        Order order = new Order();
        order.setOrderSn(orderItemInfoDto.getOrderSn());
        List<Order> orderList = orderService.selectOrderList(order);
        Long orderId = null;
        if (CollectionUtil.isNotEmpty(orderList) && orderList.size() == 1) {
            orderId = orderList.get(0).getOrderId();
        }else {
            log.info("订单{}在订单表不存在或者存在多条记录", orderItemInfoDto.getOrderSn());
            return;
        }
        order = new Order();
        order.setOrderId(orderId);
        order.setSellerOrderStatus(orderItemInfoDto.getWarehouseOrderStatus());
        order.setWarehouseOrderStatus(orderItemInfoDto.getWarehouseOrderStatus());
        order.setWarehouseProcessStatus(ObjectUtil.isNotNull(orderItemInfoDto.getWarehouseProcessStatus()) ? orderItemInfoDto.getWarehouseProcessStatus() : null);
        order.setLastUpdateBy(GlobalConstant.COMMON_SYNC_TASK_USER_ID);
        order.setLastUpdateDate(new Date());
        orderService.updateOrder(order);
    }

    /**
     * 将orderList转换成orderDtoList
     *
     * @param orderList
     * @return
     */
    private List<OrderDto> mapOrderEntityListToDtoList(List<Order> orderList) {
        List<OrderDto> orderDtoList = new ArrayList<>();
        for (Order order : orderList) {
            OrderDto orderDto = orderMap.mapOrderEntityToDto(order);
            orderDtoList.add(orderDto);
        }
        return orderDtoList;
    }
}
